﻿using System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Windows.Forms;

namespace ComToKey
{
    class MyAPI
    {
        public delegate bool CallBack(IntPtr hwnd, int para); //声明委托回调函数

        //重写API函数
        //[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true,CallingConvention = CallingConvention.Winapi)]
        [System.Runtime.InteropServices.DllImport("user32", EntryPoint = "GetKeyState")]
        public static extern int GetKeyState(int intkey);

        //将创建指定窗口的线程设置到前台,并激活
        [DllImport("user32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern bool SetForegroundWindow(IntPtr hwnd);

        //获取前台窗体
        [DllImport("user32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern IntPtr GetForegroundWindow();

        //获取活动窗体
        [DllImport("user32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern IntPtr GetActiveWindow();


        //模拟鼠标事件
        [DllImport("user32.dll")]
        public static extern int mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);

        //得到窗口的句柄
        [DllImport("user32.dll")]
        public static extern IntPtr FindWindow(string classname, string windowname);

        [DllImport("user32.dll")]
        public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
        // 
        [DllImport("user32.dll")]
        public static extern IntPtr ShowWindow(IntPtr hw);

        //根据句柄得到一个剪辑区域
        [DllImport("user32.dll")]
        public static extern IntPtr GetDC(IntPtr hwnd);

        [DllImport("user32.dll")]
        public static extern Int32 ReleaseDC(IntPtr hwnd, IntPtr hdc);

        //获得指定坐标点的像素的RGB颜色值
        [DllImport("Gdi32.dll")]
        public static extern uint GetPixel(IntPtr hdc, int nXPos, int nYPos);

        //返回指定窗口的边框矩形的尺寸
        [DllImport("user32.dll")]
        public static extern bool GetWindowRect(IntPtr hWnd, ref Rectangle rect);

        [DllImport("user32.dll", EntryPoint = "keybd_event")]

        public static extern void keybd_event(
        byte bVk, //虚拟键值   
        byte bScan,// 一般为0   
        int dwFlags, //这里是整数类型 0 为按下，2为释放   
        int dwExtraInfo //这里是整数类型 一般情况下设成为0   
        );
        [DllImport("user32.dll ", EntryPoint = "SendMessage")]
        public static extern IntPtr SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);

        [DllImport("user32.dll ", EntryPoint = "PostMessage")]
        public static extern bool PostMessage(IntPtr hWnd, int Msg, int wParam, int lParam);


        //遍历窗口
        [DllImport("user32")]
        public static extern int EnumWindows(CallBack x, int y);
        //窗口是否显示
        [DllImport("user32")]
        public static extern bool IsWindowVisible(IntPtr hwnd);
        //查找句柄的标题
        [DllImport("user32")]
        public static extern int GetWindowText(IntPtr hwnd, StringBuilder lpString, int cch);
        //根据句柄返回标题的长度
        [DllImport("user32")]
        public static extern int GetWindowTextLength(IntPtr hwnd);

        //static int MOUSEEVENTF_MOVE = 0x0001;        //移动鼠标
        //static int MOUSEEVENTF_LEFTDOWN = 0x0002;    //模拟鼠标左键按下
        //static int MOUSEEVENTF_LEFTUP = 0x0004;      //模拟鼠标左键抬起
        //static int MOUSEEVENTF_RIGHTDOWN = 0x0008;   //模拟鼠标右键按下
        //static int MOUSEEVENTF_RIGHTUP = 0x0010;     //模拟鼠标右键抬起
        //static int MOUSEEVENTF_MIDDLEDOWN = 0x0020;  //模拟鼠标中键按下
        //static int MOUSEEVENTF_MIDDLEUP = 0x0040;    //模拟鼠标中键抬起
        //static int MOUSEEVENTF_ABSOLUTE = 0x8000;    //标示是否采用绝对坐标 
        public const  int WM_KEYDOWN = 0x0100;
        public const  int WM_KEYUP = 0x0101;
        public const int WM_CHAR = 0x0101;

        public const int Key_M = 0x4D;
        public const int Key_Left_Windows = 0x5B;
        public const int Key_Space = 0x20;
        public const int Key_PGUP = 0x21;
        public const int Key_PGDN = 0x22;
    }
}
